﻿using System;
using System.Globalization;

namespace Microsoft.International.Formatters
{

    /// <summary>
    /// Spanish (es-ES) formatter
    /// </summary>
    [FormatCultureInfoAttribute("es-ES")]
    internal sealed class ESStdFormatter : EuropeFormatter
    {

        /// <summary>
        /// Returns the Decimal value to String.
        /// </summary>
        /// <param name="num">Value to translate.</param>
        /// <returns>Returns the specified Decimal value to String.</returns>
        /// <remarks>Abstract method, must be implemented.</remarks>
        internal override String ConvertToLocalizedText(Decimal num)
        {
            String strMoins = String.Empty;
            Int32 value = (Int32)num; // TODO: Supprimer cette ligne
            if (value < 0)
            {
                strMoins = "menos ";
                if (value == Int32.MinValue)
                {
                    strMoins += "dos mil milliones ";
                    value += 2000000000;
                }
                value = Math.Abs(value);
            }

            return strMoins + MillionToString(value);
        }

        /// <summary>
        /// Convert units (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String UnitToString(Int32 value)
        {
            if (value > 9)
                return TenToString(value);

            String result = String.Empty;

            switch (value.ToString(new CultureInfo("es-ES")))
            {
                case "0": result = "cero"; break;
                case "1": result = "uno"; break;
                case "2": result = "dos"; break;
                case "3": result = "tres"; break;
                case "4": result = "cuatro"; break;
                case "5": result = "cinco"; break;
                case "6": result = "seis"; break;
                case "7": result = "siete"; break;
                case "8": result = "ocho"; break;
                case "9": result = "nueve"; break;
            }

            return result;
        }

        /// <summary>
        /// Convert tens (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String TenToString(Int32 value)
        {
            if (value < 10)
                return UnitToString(value);
            else if (value > 99)
                return HundredToString(value);

            String result = String.Empty;

            switch (value)
            {
                case 10: result = "diez"; break;
                case 11: result = "once"; break;
                case 12: result = "doce"; break;
                case 13: result = "trece"; break;
                case 14: result = "catorce"; break;
                case 15: result = "quince"; break;
                case 16: result = "dieciséis"; break;
                case 17: result = "diecisiete"; break;
                case 18: result = "dieciocho"; break;
                case 19: result = "diecinueve"; break;
                case 20: result = "veinte"; break;
                case 21: result = "veintiuno"; break;
                case 22: result = "veintidós"; break;
                case 23: result = "veintitrés"; break;
                case 24: result = "veinticuatro"; break;
                case 25: result = "veinticinco"; break;
                case 26: result = "veintiséis"; break;
                case 27: result = "veintisiete"; break;
                case 28: result = "veintiocho"; break;
                case 29: result = "veintinueve"; break;

                case 30: result = "treinta"; break;
                case 40: result = "cuarenta"; break;
                case 50: result = "cincuenta"; break;
                case 60: result = "sesenta"; break;
                case 70: result = "setenta"; break;
                case 80: result = "ochenta"; break;
                case 90: result = "noventa"; break;
                default:
                    result = String.Format(new CultureInfo("es-ES"), "{0} y {1}", TenToString(value.GetTen()), UnitToString(value.GetUnit()));
                    break;
            }

            return result;
        }

        /// <summary>
        /// Convert hundreds (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String HundredToString(Int32 value)
        {
            if (value < 100)
                return TenToString(value);
            else if (value > 999)
                return ThousandToString(value);

            Int32 reste = 0;
            Int32 centaine = System.Math.DivRem(value, 100, out reste);

            String ciento = String.Empty;

            switch (centaine)
            {
                case 1: ciento = "ciento"; break;
                case 2: ciento = "doscientos "; break;
                case 3: ciento = "trescientos"; break;
                case 4: ciento = "cuatrocientos"; break;
                case 5: ciento = "quinientos"; break;
                case 6: ciento = "seiscientos"; break;
                case 7: ciento = "setecientos"; break;
                case 8: ciento = "ochocientos"; break;
                case 9: ciento = "novecientos"; break;
            }

            if (reste == 0)
                return ciento;
            else
                return String.Format(new CultureInfo("es-ES"), "{0} {1}", ciento, TenToString(reste));
        }

        /// <summary>
        /// Convert thousands (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String ThousandToString(Int32 value)
        {
            if (value < 1000)
                return HundredToString(value);
            else if (value > 999999)
                return MillionToString(value);

            Int32 reste = 0;
            Int32 millier = System.Math.DivRem(value, 1000, out reste);

            String strMillier = "mil";
            if (millier > 1)
                strMillier = String.Format(new CultureInfo("es-ES"), "{0} mil", HundredToString(millier));

            if (reste == 0) return strMillier;
            return String.Format(new CultureInfo("es-ES"), "{0} {1}", strMillier, HundredToString(reste));
        }

        /// <summary>
        /// Convert millions (Int32 format) to String format.
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <returns>Returns a text which represents the specified value</returns>
        private String MillionToString(Int32 value)
        {
            if (value < 1000000)
                return HundredToString(value);

            Int32 reste = 0;
            Int32 millions = System.Math.DivRem(value, 1000000, out reste);

            String strMillions = "un millón";

            if (millions > 1)
                strMillions = String.Format(new CultureInfo("es-ES"), "{0} milliones", ThousandToString(millions));

            if (reste > 0)
                strMillions = String.Format(new CultureInfo("es-ES"), "{0} {1}", strMillions, ThousandToString(reste));

            return strMillions;
        }

    }

}